Program Structure
Every Go program, no matter how big or small, follows a few key rules:
-
Package Declaration – Tells Go what “package” this file belongs to.
- If the program is meant to run directly, the package must be main.
-
Import Statements – Brings in other code (libraries) you want to use.
-
Functions – Blocks of code that do things.
- The main function is special: it’s where your program starts running.
-
Statements & Expressions – The actual instructions you give the computer
General Layout:
package main // 1. Package declaration
import "fmt" // 2. Import statement
// 3. Function definition
func main() {
fmt.Println("Hello, World!") // 4. Statements
}
Key Points to Remember
- Go is case-sensitive (
Mainis different frommain). - Every executable Go program starts with:
package mainfunc main() { ... }